from utils.excel_util import ExcelUtil
from model.sysLogininforModel import SysLogininfor
from schema.sysLogininforSchema import SysLogininforCreate
from typing import List, Any, Dict, ClassVar
from io import BytesIO
from datetime import datetime


class SysLogininforExcel:
    # Define export columns
    COLUMNS: ClassVar[List[Dict[str, Any]]] = [

        {"field": "user_name", "title": "用户账号", "required": False},

        {"field": "ipaddr", "title": "登录IP地址", "required": False},

        {"field": "login_location", "title": "登录地点", "required": False},

        {"field": "browser", "title": "浏览器类型", "required": False},

        {"field": "os", "title": "操作系统", "required": False},

        {"field": "login_status", "title": "登录状态（0成功 1失败）", "required": False},

        {"field": "status", "title": "状态（0正常 2删除）", "required": False},

        {"field": "msg", "title": "提示消息", "required": False},

        {"field": "login_time", "title": "访问时间", "required": False},

        {"field": "create_by", "title": "创建者", "required": False},

        {"field": "create_time", "title": "创建时间", "required": False},

        {"field": "update_by", "title": "更新者", "required": False},

        {"field": "update_time", "title": "更新时间", "required": False},

    ]

    @classmethod
    def get_export_columns(cls) -> List[Dict[str, Any]]:
        return [

            {"field": "user_name", "title": "用户账号"},

            {"field": "ipaddr", "title": "登录IP地址"},

            {"field": "login_location", "title": "登录地点"},

            {"field": "browser", "title": "浏览器类型"},

            {"field": "os", "title": "操作系统"},

            {"field": "login_status", "title": "登录状态（0成功 1失败）"},

            {"field": "status", "title": "状态（0正常 2删除）"},

            {"field": "msg", "title": "提示消息"},

            {"field": "login_time", "title": "访问时间"},

            {"field": "create_by", "title": "创建者"},

            {"field": "create_time", "title": "创建时间"},

            {"field": "update_by", "title": "更新者"},

            {"field": "update_time", "title": "更新时间"},

        ]

    @classmethod
    def get_import_template(cls) -> BytesIO:
        """Get import template"""
        example_data = [{

            "user_name": "example",

            "ipaddr": "example",

            "login_location": "example",

            "browser": "example",

            "os": "example",

            "login_status": "A",

            "status": "A",

            "msg": "example",

            "login_time": datetime.now(),

            "create_by": "example",

            "create_time": datetime.now(),

            "update_by": "example",

            "update_time": datetime.now(),

        }]
        return ExcelUtil.create_import_template(
            columns=cls.COLUMNS,
            example_data=example_data,
            sheet_name="系统访问记录导入模板"
        )

    @classmethod
    async def export_data(cls, data: List[Any]) -> BytesIO:
        """Export data"""
        return await ExcelUtil.export_data(
            data=data,
            columns=cls.get_export_columns(),
            sheet_name="系统访问记录数据"
        )

    @classmethod
    def parse_import_data(cls, file: BytesIO) -> List[Dict[str, Any]]:
        """Parse import data"""
        return ExcelUtil.parse_import_data(
            file=file,
            columns=cls.COLUMNS,
            schema_cls=SysLogininforCreate
        )